/**
 * author: MaHX
 * date: 2019/8/18/22:29
 * description: 自定义ArrayList
 **/
public class MyArrayList {
    private Object[] objs = new Object[4];
    int size = 0;

    public int size() {
        return size;

    }

    public void add(Object value) {
//        判断数组长度
        if (size >= objs.length) {
            Object[] arrs = new Object[size * 2];
            for (int i = 0; i < size; i++) {
                arrs[i] = objs[i];
            }
            objs = arrs;
        }
        objs[size] = value;
        size++;
    }

    public void set(int index, Object value) throws Exception {
        if (index<0 || index>=size){
            throw new Exception("数组越界");
        }
        objs[index] = value;

    }

    public Object get(int index) throws Exception {
        if (index<0 || index>=size){
            throw new Exception("数组越界");
        }
        return objs[index];
    }

    public void clear() {
        size = 0;
        objs = new Object[4];
    }

    public void removeAt(int index) throws Exception {
        if (index<0 || index>=size){
            throw new Exception("数组越界");
        }
        for (int i = index+1; i < size; i++) {
            objs[i-1] = objs[i];
        }
        size--;
    }
}
